const Base = require('./base');

module.exports = class extends Base {
  async listAction() {
    const menuId = this.request.query.menu_id;
    if (saas.isEmpty(menuId)) {
      const data = await this.mysql('hanyu_menu').where({is_menu: 1});
      data.forEach(menu => {
        if (saas.isEmpty(menu.parent_id) && menu.is_menu) {
          menu.children = data.filter(p => p.parent_id === menu.id);
        }
      })
      return this.success(data.filter(p => saas.isEmpty(p.parent_id)));
    } else {
      const data = await this.mysql('hanyu_menu').where({parent_id: menuId, is_action: 1});
      return this.success(data);
    }
  }
  
  async editAction() {
    const model = this.request.body;
    await this.mysql.thenUpdate('hanyu_menu', {id: model.id || null}, model);
    return this.success();
  }
  
  async delAction() {
    const id = this.request.body.id;
    await this.mysql('hanyu_menu').where({id: id}).del();
    return this.success();
  }
  
  async updateIsShowAction() {
    const id = this.request.body.id;
    const isShow = this.request.body.is_show;
    await this.mysql('hanyu_menu').where({id: id}).update({is_show: isShow});
    return this.success();
  }
}